﻿using CardMonitor.Model;
using CardMonitor.Utils;
using CardMonitor.Windows;
using DotNetSpeech;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace CardMonitor.Pages
{
    /// <summary>
    /// PrintItemPage.xaml 的交互逻辑
    /// </summary>
    public partial class PrintItemPage : Page
    {
        CardInfo objIDCardInfo = null;
        MainWindow mainWindow = null;
        BillModel model = null;
        SpVoice speech = new SpVoice();
        public PrintItemPage(MainWindow main, BillModel billModel)
        {
            InitializeComponent();
            mainWindow = main;
            model = billModel;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            String tag = (String)((ListBoxItem)Listbox.SelectedItem).Tag;
            if (tag.Equals("01"))
            {
                doPrintQualityReport(model, "2");
            }
            else if (tag.Equals("02"))
            {
                doPrint(model,"1");
            }

        }

        private void doPrintQualityReport(BillModel billModel, string type)
        {
            if (App.getConfig().IfPrint.Equals("0"))
            {
                PrintWindow printWindow = new PrintWindow();
                printWindow.setBillMode(billModel);
                if ("1".Equals(billModel.Type))
                {
                    printWindow.setTemplete("tplSaleAccountS", type);
                }
                else
                {
                    printWindow.setTemplete("tplBuyAccountS", type);
                }
                printWindow.Owner = mainWindow;
                printWindow.ShowDialog();
            }
        }

        private void doPrint(BillModel billModel,string type)
        {

            long id = billModel.Id;
            string t = "1";

            HttpPostWindow httpPostWindow = new HttpPostWindow();
            httpPostWindow.Owner = mainWindow;
            JObject paramJSON = new JObject();
            paramJSON.Add("t", t);
            paramJSON.Add("id", id);
            httpPostWindow.setParamObject(paramJSON);
            httpPostWindow.setOperation(HttpPostWindow.OperationType.queryBillPrintCount.ToString());
            bool result = (bool)httpPostWindow.ShowDialog();
            if (!result)
            {
                string message = "结算失败,请到人工窗口办理";
                showMessage(message);
                return;
            }
            JObject resultObj = httpPostWindow.getResultObject();
            if (null == resultObj)
            {
                showMessage("发生错误");
                return;
            }
            string printCount = resultObj["count"].ToString();
            if (!"1".Equals(printCount))
            {
                showMessage("超打印次数,请到人工窗口");
                return;
            }
            if (App.getConfig().IfPrint.Equals("0"))
            {
                PrintWindow printWindow = new PrintWindow();
                printWindow.setBillMode(billModel);
                if ("1".Equals(billModel.Type))
                {
                    printWindow.setTemplete("tplSaleAccountS","1");
                }
                else
                {
                    printWindow.setTemplete("tplBuyAccountS","1");
                }
                printWindow.Owner = mainWindow;
                printWindow.ShowDialog();
            }
        }

        private void showMessage(String message)
        {
            InfoWindow infoWindow = new InfoWindow(message);
            infoWindow.Owner = mainWindow;
            infoWindow.ShowDialog();
        }
        private void CloseWin(object sender, RoutedEventArgs e)
        {
            NavigationService.GetNavigationService(this).GoBack();
            //Window win = (Window)this.Parent;
            //win.Close();
        }
    }
}
